#include "TFViewImpl.h"
#include "TFMenu.h"

#include "TFServices.h"


using namespace std;

//Note: normally I would access the board as (y,x) since thats equivalent to row,column
//      but to conform to what the rest is like I'll go ahead and change it to (x,y)

// so.. X= row, Y= column
TFViewImpl::TFViewImpl()
{
	PA_InitText(SCREEN_TOP, 0);
    //set color of text to gray
	PA_SetTextCol(SCREEN_TOP, 0,0,0);
	
	_model = NULL;
	_menuView = new TFMenuView();

	gameViewInitiated = false;
}

TFViewImpl::~TFViewImpl()
{
	_model = NULL;
	if(_menuView)delete _menuView; _menuView = NULL;
}

HRESULT TFViewImpl::DrawCurrentSplash(TF_GAME_MODE gameMode)
{
	PA_DeleteBg(0, 3);
	PA_DeleteBg(1, 3);
	PA_ClearTextBg(0);
	//TFServices::FadeBgOut(SCREEN_BOTH);

	switch(gameMode)
	{
		//PA_EasyBgLoad(SCREENINDEX(1=top, 0=bottom),
		//				BGLAYERINDEX,
		//				POINTERTOBG);
		case SPLASH_WARNING:
			PA_EasyBgLoad(1, 3, warning);
			PA_EasyBgLoad(0, 3, rating);
			break;
		case SPLASH_UNCC:
			PA_EasyBgLoad(1, 3, gddLogo);
			PA_OutputText(0, 9,7, "UNC Charlotte");
			PA_OutputText(0, 2,9, "Game Design and Development");
			PA_OutputText(0, 8,11, "Program presents...");
			break;
		case SPLASH_LOGO:
			PA_EasyBgLoad(1, 3, dojoLogo);
			PA_OutputText(0, 4,10, "A RapidDojo production...");
			break;
		case SPLASH_MAIN:
			PA_EasyBgLoad(1, 3, troopsLogo);
			PA_EasyBgLoad(0, 3, troopsLogo_bottom);
			break;
		default:
			break;
	}
	//TFServices::FadeBgIn(SCREEN_BOTH);

	return S_OK;
}

HRESULT TFViewImpl::DrawCurrentMenu()
{
	_menuView->Redraw();
	return S_OK;
}

void TFViewImpl::SetModel(TFModelInterface * iTFModel)
{
	_model = iTFModel;
	_menuView->SetModel(_model);
}

HRESULT TFViewImpl::BoardClicked(int stylusX, int stylusY, int *row, int *col)
{
    if (stylusX > BOARD_SQUARE_SIZE*BOARD_DIMENSION)
    {
        return E_FAIL;
    }
    else
    {
        *row = int( stylusY / BOARD_SQUARE_SIZE);
        *col = int( stylusX / BOARD_SQUARE_SIZE);
		DeselectButton();
        return S_OK;
    }
}

HRESULT TFViewImpl::ButtonClicked(int stylusX, int stylusY, int *index)
{
    if (stylusX <= BOARD_SQUARE_SIZE*BOARD_DIMENSION)
    {
        return E_FAIL;
    }
    else
    {
		for (unsigned int i=0; i < buttonList.size(); i++)
		{
			if (stylusX > buttonList[i].originX && stylusY > buttonList[i].originY &&
				stylusX < buttonList[i].originX + buttonList[i].width && 
				stylusY < buttonList[i].originY + buttonList[i].height)
			{
				if (buttonList[i].index == BTN_RAND)
				{
					if (_model->DoInitSetup() == false)
					{
						return E_FAIL;
					}
				}
				*index= buttonList[i].index;
				//show the selector box
				PA_SetSpriteXY(0, btnSelectSpriteIndex, buttonList[i].originX, buttonList[i].originY);
				
				return S_OK;
			}
		}
        *index=-1;//not found
        return E_FAIL;
    }
}

HRESULT TFViewImpl::Redraw()
{
	HRESULT HR = S_OK;

	//Get information from the model
	//Draw stuff on the screen
	TF_GAME_MODE gameMode = _model->GetGameMode();
	//gameMode = GAME;

	if( gameMode == SPLASH_WARNING ||
		gameMode == SPLASH_UNCC ||
		gameMode == SPLASH_LOGO ||
		gameMode == SPLASH_MAIN)
	{
		DrawCurrentSplash(gameMode);
	}
	else if( gameMode == MENU )
	{
		_menuView->Redraw();
	}
	else if ( gameMode == GAME_ONEPLAYER || gameMode == GAME_TWOPLAYER)
	{
		DrawGameMode();
	}
	
	return HR;
}

HRESULT TFViewImpl::DrawPieceLocation(int col, int row, bool isSelected)
{
	return E_FAIL;
}

void TFViewImpl::DrawRulesScreen()
{
	HideAllSprites();

	PA_EasyBgLoad(0, 3, howToPlay);
	PA_ClearTextBg(0);

	howtoScrollValue = 0;

	//prevents it from reading the Start button that was pressed
	//	to activate this screen in the first place...
	PA_WaitForVBL();
	while(!Pad.Newpress.Start && !Pad.Newpress.B)
	{		
		if (Stylus.Newpress)
		{
			oldStylusX = Stylus.X;
			oldStylusY = Stylus.Y;
			oldScrollValue = howtoScrollValue;
		}
		else if (Stylus.Held)
		{
			howtoScrollValue = oldScrollValue + (oldStylusY - Stylus.Y);
		}
		howtoScrollValue += (Pad.Held.Down - Pad.Held.Up)*4;
		if(howtoScrollValue < 0) {
			howtoScrollValue = 0;
		}
		else if(howtoScrollValue > 1200) {
			howtoScrollValue = 1200;
		}
		
		PA_LargeScrollY(0, 3, howtoScrollValue);
		PA_WaitForVBL();
	}

	UndoHideSprites();
}

bool TFViewImpl::GetConfirmQuit()
{
	HideAllSprites();

	PA_DeleteBg(0, 3);
	PA_ClearTextBg(0);

	PA_EasyBgLoad(0, 3, quit_bottom);


	while(1)
	{		
		if (Stylus.Newpress)
		{
			//check for button click
			if (Stylus.X > CONFIRMQUIT_YES_X1 && Stylus.X < CONFIRMQUIT_YES_X2 &&
				Stylus.Y > CONFIRMQUIT_YES_Y1 && Stylus.Y < CONFIRMQUIT_YES_Y2)
			{
				UndoHideSprites();
				return true;
			}
			if (Stylus.X > CONFIRMQUIT_NO_X1 && Stylus.X < CONFIRMQUIT_NO_X2 &&
				Stylus.Y > CONFIRMQUIT_NO_Y1 && Stylus.Y < CONFIRMQUIT_NO_Y2)
			{
				UndoHideSprites();
				return false;
			}
		}
		if (Pad.Newpress.A)
		{
			//means yes!
			UndoHideSprites();
			return true;
		}
		if (Pad.Newpress.B)
		{
			//means NO
			UndoHideSprites();
			return false;
		}
		PA_WaitForVBL();
	}
	UndoHideSprites();
	return false;
}

void TFViewImpl::DisplayWinScreen(int player)
{
	PA_DeleteBg(0, 3);
	PA_DeleteBg(1, 3);
	PA_ClearTextBg(0);
	PA_ClearTextBg(1);
	
	if (player == RED_PLAYER)
	{
		PA_EasyBgLoad(1, 3, win_red_top);
		PA_EasyBgLoad(0, 3, win_red_bottom);
	}
	else
	{
		PA_EasyBgLoad(1, 3, win_blue_top);
		PA_EasyBgLoad(0, 3, win_blue_bottom);
	}

	while(!Stylus.Newpress && !Pad.Newpress.Start && !Pad.Newpress.A && !Pad.Newpress.B)
	{
		PA_WaitForVBL();
	}
}

void TFViewImpl::DrawGameMode()
{
	if(gameViewInitiated == false){
		GameViewInit();
		gameViewInitiated = true;
	}

	
	PA_DeleteBg(0, 3);
	PA_DeleteBg(1, 3);
	PA_ClearTextBg(0);

	if (_model->GetTurn() == RED_PLAYER)
	{
		PA_EasyBgLoad(1, 3, gametop_red);
	}
	else
	{
		PA_EasyBgLoad(1, 3, gametop_blue);
	}
	PA_EasyBgLoad(0, 3, board);

	PA_SetTextCol(0,31,31,0);
	PA_OutputText(0,29,6, "%d", _model->GetNumTroops());
	PA_OutputText(0,29,10, "%d", _model->GetNumBarricades());
	PA_OutputText(0,29,14, "%d", _model->GetNumFlags());

	int spriteIndex=0;

	//PA_OutputText(1,0,1, "Cord X: %d", Stylus.X);
	//PA_OutputText(1,0,3, "Cord Y: %d", Stylus.Y);

	//PA_SetTextCol(1,31,31,31);
	//PA_OutputText(1,0,6, "Action Points: %d", _model->GetGameTurn()->GetPoints());
	
	int selectX = _model->GetGameMove()->GetX1();
	int selectY = _model->GetGameMove()->GetY1();

	if (_model->GetSelected() != SELECTED)
	{
		PA_SetSpriteXY(0, selectedSpriteIndex, -50,-50);
		//clear move costs
		PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+1, " ");
		PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+6, " ");
		PA_OutputText(1, (MOVE_GRID_POS_X/8)+1, (MOVE_GRID_POS_Y / 8)+3, " ");
		PA_OutputText(1, (MOVE_GRID_POS_X/8)+7, (MOVE_GRID_POS_Y / 8)+3, " ");
	}
	for (int x=0; x < BOARD_DIMENSION; x++)//column
	{
		for (int y=0; y < BOARD_DIMENSION; y++)//row
		{
            spriteIndex = x * BOARD_DIMENSION + y;
			TFGamePiece* piece= _model->GetBoard()->GetPieceAt(x,y);
			TF_PIECE_STATE pieceType = piece->GetPiece();
			TF_PLAYERS color = piece->GetColor();

			if (pieceType != EMPTY)
			{
				PA_UpdateSpriteGfx(0, spriteIndex, spriteInfo[color][pieceType].spriteName);
				PA_SetSpritePal(0, spriteIndex, spriteInfo[color][pieceType].palIndex);
			}
			else
			{
				PA_UpdateSpriteGfx(0, spriteIndex, (void*)emptysprite_Sprite);
				PA_SetSpritePal(0, spriteIndex, 0);
			}
			if (x == selectX && y == selectY)
			{
				if (_model->GetSelected() == SELECTED)
				{
					PA_SetSpriteXY(0, selectedSpriteIndex, x*24, y*24);
                    
                    //also update the top screen with costs
                    TFGamePiece* checkPiece;
                    int cost;
                    //Up
                    checkPiece = _model->GetBoard()->GetPieceAt(x,y-1);
                    if (checkPiece != NULL)
                    {
                        cost = _ruleChecker->CostToCapture(checkPiece, color);
                        if (cost == 999)
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+1, "X");
                        }
                        else
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+1, "%d",cost);
                        }
                    }
                    //Down
                    checkPiece = _model->GetBoard()->GetPieceAt(x,y+1);
                    if (checkPiece != NULL)
                    {
                        cost = _ruleChecker->CostToCapture(checkPiece, color);
                        if (cost == 999)
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+6, "X");
                        }
                        else
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+6, "%d",cost);
                        }
                    }
                    //Left
                    checkPiece = _model->GetBoard()->GetPieceAt(x-1,y);
                    if (checkPiece != NULL)
                    {
                        cost = _ruleChecker->CostToCapture(checkPiece, color);
                        if (cost == 999)
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+1, (MOVE_GRID_POS_Y / 8)+3, "X");
                        }
                        else
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+1, (MOVE_GRID_POS_Y / 8)+3, "%d",cost);
                        }
                    }
                    //Right
                    checkPiece = _model->GetBoard()->GetPieceAt(x+1,y);
                    if (checkPiece != NULL)
                    {
                        cost = _ruleChecker->CostToCapture(checkPiece, color);
                        if (cost == 999)
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+7, (MOVE_GRID_POS_Y / 8)+3, "X");
                        }
                        else
                        {
                            PA_OutputText(1, (MOVE_GRID_POS_X/8)+7, (MOVE_GRID_POS_Y / 8)+3, "%d",cost);
                        }
                    }
				}
			}
		}
	}
}

void TFViewImpl::InitTopSpriteList()
{
	////////////////////////////////////////////////]
	//Top screen

	PA_LoadSpritePal(1, // Screen
			MOVE_GRID, // Palette number
			(void*)gamegrid1_Pal);// Palette name

	PA_LoadSpritePal(1, // Screen
			ACTIONPTS_PAL, // Palette number
			(void*)numbers_Pal);// Palette name

	PA_LoadSpritePal(1, // Screen
			PLACE_PIECE_PAL, // Palette number
			(void*)placePieces_Pal);// Palette name
}


void TFViewImpl::InitSpriteList()
{
	const int RED = 0;
	const int BLUE= 1;

	//setup palettes
	//Empty space
	palInfo[EMPTY_SPRITE].palName = (void*)emptysprite_Pal;
	palInfo[EMPTY_SPRITE].palType= EMPTY_SPRITE;

	PA_LoadSpritePal(0, // Screen
			EMPTY_SPRITE, // Palette number
			(void*)emptysprite_Pal);// Palette name

	//Red troop
	palInfo[RED_TROOP].palName = (void*)redtroop1_Pal;
	palInfo[RED_TROOP].palType= RED_TROOP;

	PA_LoadSpritePal(0, // Screen
			RED_TROOP, // Palette number
			(void*)redtroop1_Pal);// Palette name

	palInfo[BLUE_TROOP].palName= (void*)bluetroop1_Pal;
	palInfo[BLUE_TROOP].palType= BLUE_TROOP;

	PA_LoadSpritePal(0, // Screen
			BLUE_TROOP, // Palette number
			(void*)bluetroop1_Pal);// Palette name
    
        //Red troop
    palInfo[RED_FLAG].palName = (void*)redflag1_Pal;
    palInfo[RED_FLAG].palType= RED_FLAG;
    
    PA_LoadSpritePal(0, // Screen
             RED_FLAG, // Palette number
             (void*)redflag1_Pal);// Palette name
        
    palInfo[BLUE_FLAG].palName= (void*)blueflag1_Pal;
    palInfo[BLUE_FLAG].palType= BLUE_FLAG;
    
    PA_LoadSpritePal(0, // Screen
             BLUE_FLAG, // Palette number
             (void*)blueflag1_Pal);// Palette name


	palInfo[RED_BARRICADE].palName= (void*)redbarricade1_Pal;
	palInfo[RED_BARRICADE].palType= RED_BARRICADE;

	PA_LoadSpritePal(0, // Screen
			RED_BARRICADE, // Palette number
			(void*)redbarricade1_Pal);// Palette name


	palInfo[BLUE_BARRICADE].palName= (void*)bluebarricade1_Pal;
	palInfo[BLUE_BARRICADE].palType= BLUE_BARRICADE;

	PA_LoadSpritePal(0, // Screen
		BLUE_BARRICADE, // Palette number
		(void*)bluebarricade1_Pal);// Palette name

	palInfo[BUTTONPAL].palName= (void*)btnquit_Pal;
	palInfo[BUTTONPAL].palType= BUTTONPAL;

	PA_LoadSpritePal(0, // Screen
		BUTTONPAL, // Palette number
		(void*)btnrandom_Pal);// Palette name

	palInfo[INACTIVEBUTTON_PAL].palName= (void*)btntroop_inactive_Pal;
	palInfo[INACTIVEBUTTON_PAL].palType= INACTIVEBUTTON_PAL;

	PA_LoadSpritePal(0, // Screen
		INACTIVEBUTTON_PAL, // Palette number
		(void*)btntroop_inactive_Pal);// Palette name

	palInfo[SELECTED_PAL].palName= (void*)selected_Pal;
	palInfo[SELECTED_PAL].palType= SELECTED_PAL;

	PA_LoadSpritePal(0, // Screen
		SELECTED_PAL, // Palette number
		(void*)selected_Pal);// Palette name

	palInfo[BTNSELECT_PAL].palName= (void*)btnselect_Pal;
	palInfo[BTNSELECT_PAL].palType= BTNSELECT_PAL;

	PA_LoadSpritePal(0, // Screen
		BTNSELECT_PAL, // Palette number
		(void*)btnselect_Pal);// Palette name

	for (int color=0; color < 2; color++)
	{
		for (int i=0; i < 8; i++)
		{
			spriteInfo[color][i].pieceType=i;
			switch(i)
			{
			case EMPTY:
				spriteInfo[color][i].spriteName = (void*)emptysprite_Sprite;
				spriteInfo[color][i].palIndex = EMPTY_SPRITE;
				break;
			case FLAG_0:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redflag1_Sprite;
					spriteInfo[color][i].palIndex = RED_FLAG;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)blueflag1_Sprite;
					spriteInfo[color][i].palIndex = BLUE_FLAG;
				}
				break;
			
			case FLAG_1:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redflag2_Sprite;
					spriteInfo[color][i].palIndex = RED_FLAG;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)blueflag2_Sprite;
					spriteInfo[color][i].palIndex = BLUE_FLAG;
				}
				break;
			
			case FLAG_2:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redflag3_Sprite;
					spriteInfo[color][i].palIndex = RED_FLAG;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)blueflag3_Sprite;
					spriteInfo[color][i].palIndex = BLUE_FLAG;
				}
				break;
			
			case BARRICADE_1:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redbarricade1_Sprite;
					spriteInfo[color][i].palIndex = RED_BARRICADE;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)bluebarricade1_Sprite;
					spriteInfo[color][i].palIndex = BLUE_BARRICADE;
				}
				break;
			
			case BARRICADE_2:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redbarricade2_Sprite;
					spriteInfo[color][i].palIndex = RED_BARRICADE;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)bluebarricade2_Sprite;
					spriteInfo[color][i].palIndex = BLUE_BARRICADE;
				}
				break;
			
			case BARRICADE_3:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redbarricade3_Sprite;
					spriteInfo[color][i].palIndex = RED_BARRICADE;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)bluebarricade3_Sprite;
					spriteInfo[color][i].palIndex = BLUE_BARRICADE;
				}
				break;
			
			case TROOP_PIECE:
				if (color == RED)
				{
					spriteInfo[color][i].spriteName = (void*)redtroop1_Sprite;
					spriteInfo[color][i].palIndex = RED_TROOP;
				}
				else if (color == BLUE)
				{
					spriteInfo[color][i].spriteName = (void*)bluetroop1_Sprite;
					spriteInfo[color][i].palIndex = BLUE_TROOP;
				}
				break;
			}
		}
	}

	int spriteIndex = BUTTON_SPRITES_START;
	for (unsigned int i=0; i < buttonList.size(); i++)
	{
		PA_CreateSprite(0, // Screen
				spriteIndex, // Sprite number
				buttonList[i].spriteName, // Sprite name
				OBJ_SIZE_32X32, // Sprite size
				1, // 256 color mode
				BUTTONPAL, // Sprite palette number
				buttonList[i].originX, buttonList[i].originY); // X and Y position on the screen
		spriteIndex++;
	}
}

void TFViewImpl::GameViewInit(){

	
	buttonList.clear();
	//add buttons.. we can clean this up later
	Button btnTemp;
	
	btnTemp.originX = 200;
	btnTemp.originY = 160;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btnrandom_Sprite;
	btnTemp.inactiveSpriteName = (void*)btnrandom_Sprite;
	btnTemp.index=BTN_RAND;

	buttonList.push_back(btnTemp);

	btnTemp.originX = 225;
	btnTemp.originY = 160;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btnrules_Sprite;
	btnTemp.inactiveSpriteName = (void*)btnrules_Sprite;
	btnTemp.index=BTN_RULES;

	buttonList.push_back(btnTemp);    

    btnTemp.originX = 220;
	btnTemp.originY = 10;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btnquit_Sprite;
	btnTemp.inactiveSpriteName = (void*)btnquit_Sprite;
	btnTemp.index=BTN_QUIT;
    
	buttonList.push_back(btnTemp);

	btnTemp.originX = 200;
	btnTemp.originY = 40;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btntroop_Sprite;
	btnTemp.inactiveSpriteName = (void*)btntroop_inactive_Sprite;
	btnTemp.index=BTN_TROOP;

	buttonList.push_back(btnTemp);

	btnTemp.originX = 200;
	btnTemp.originY = 70;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btnbarricade_Sprite;
	btnTemp.inactiveSpriteName = (void*)btnbarricade_inactive_Sprite;
	btnTemp.index=BTN_BARRICADE;

	buttonList.push_back(btnTemp);

	btnTemp.originX = 200;
	btnTemp.originY = 100;
	btnTemp.width = 24;
	btnTemp.height= 24;
	btnTemp.spriteName = (void*)btnflag_Sprite;
	btnTemp.inactiveSpriteName = (void*)btnflag_inactive_Sprite;
	btnTemp.index=BTN_FLAG;

	buttonList.push_back(btnTemp);

	InitSpriteList();
	InitTopSpriteList();
	

	int spriteCount=0;
	for (int x=0; x < BOARD_DIMENSION; x++)//column
	{
		for (int y=0; y < BOARD_DIMENSION; y++)//row
		{
				PA_CreateSprite(0, // Screen
						spriteCount, // Sprite number
						(void*)emptysprite_Sprite, // Sprite name
						OBJ_SIZE_32X32, // Sprite size
						1, // 256 color mode
						0, // Sprite palette number
						x*24,y*24); // X and Y position on the screen
				spriteCount++;
		}
	}
	selectedSpriteIndex=64;
	selectedSpritePalIndex= SELECTED_PAL;

	PA_CreateSprite(0, // Screen
		selectedSpriteIndex, // Sprite number
		(void*)selected_Sprite, // Sprite name
		OBJ_SIZE_32X32, // Sprite size
		1, // 256 color mode
		selectedSpritePalIndex, // Sprite palette number
		-50,-50); // X and Y position on the screen

	btnSelectSpriteIndex=65;
	btnSelectSpritePalIndex= BTNSELECT_PAL;

	PA_CreateSprite(0, // Screen
		btnSelectSpriteIndex, // Sprite number
		(void*)btnselect_Sprite, // Sprite name
		OBJ_SIZE_32X32, // Sprite size
		1, // 256 color mode
		btnSelectSpritePalIndex, // Sprite palette number
		-50,-50); // X and Y position on the screen

	moveGridSpriteIndex = 1;
	moveGridPalIndex = 1;

	PA_CreateSprite(1, // Screen
		moveGridSpriteIndex, // Sprite number
		(void*)gamegrid1_Sprite, // Sprite name
		OBJ_SIZE_64X64, // Sprite size
		1, // 256 color mode
		moveGridPalIndex, // Sprite palette number
		MOVE_GRID_POS_X,MOVE_GRID_POS_Y); // X and Y position on the screen

	PA_SetSpritePrio(1, moveGridSpriteIndex, 3);

	numbersSpriteIndex = 2;
	numbersPalIndex = ACTIONPTS_PAL;

	PA_CreateSprite(1, // Screen
		numbersSpriteIndex, // Sprite number
		(void*)numbers_Sprite, // Sprite name
		OBJ_SIZE_64X64, // Sprite size
		1, // 256 color mode
		numbersPalIndex, // Sprite palette number
		ACTIONPTS_POS_X,ACTIONPTS_POS_Y); // X and Y position on the screen

	PA_SetSpritePrio(1, numbersSpriteIndex, 3);

	placePiecesIndex = 3;
	placePiecesPalIndex = PLACE_PIECE_PAL;

	PA_CreateSprite(1, // Screen
		placePiecesIndex, // Sprite number
		(void*)placePieces_Sprite, // Sprite name
		OBJ_SIZE_64X64, // Sprite size
		1, // 256 color mode
		placePiecesPalIndex, // Sprite palette number
		-100,-100); // X and Y position on the screen

	UpdateActionPoints(0);
}

void TFViewImpl::UpdateActionPoints(int points)
{
	PA_SetSpriteAnim(1, numbersSpriteIndex, points);
	PA_WaitForVBL();
}

void TFViewImpl::DeInitGame()
{
	PA_ClearTextBg(0);
	PA_ClearTextBg(1);

	PA_ResetSpriteSys();

	gameViewInitiated=false;
}

void TFViewImpl::HideAllSprites()
{
	int spriteIndex;

	//hide everything on board
	for (int x=0; x < BOARD_DIMENSION; x++)//column
	{
		for (int y=0; y < BOARD_DIMENSION; y++)//row
		{
            spriteIndex = x * BOARD_DIMENSION + y;
			PA_UpdateSpriteGfx(0, spriteIndex, (void*)emptysprite_Sprite);
			PA_SetSpritePal(0, spriteIndex, 0);
		}
	}

	//hide buttons
	spriteIndex = BUTTON_SPRITES_START;
	for (unsigned int i=0; i < buttonList.size(); i++)
	{
		PA_SetSpriteXY(0, spriteIndex, -50,-50);
		spriteIndex++;
	}

	//hide button selector
	PA_SetSpriteXY(0, btnSelectSpriteIndex, -50,-50);
	//hide selector
	PA_SetSpriteXY(0, selectedSpriteIndex, -50,-50);
	//clear move costs
	PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+1, " ");
	PA_OutputText(1, (MOVE_GRID_POS_X/8)+4, (MOVE_GRID_POS_Y / 8)+6, " ");
	PA_OutputText(1, (MOVE_GRID_POS_X/8)+1, (MOVE_GRID_POS_Y / 8)+3, " ");
	PA_OutputText(1, (MOVE_GRID_POS_X/8)+7, (MOVE_GRID_POS_Y / 8)+3, " ");
}

void TFViewImpl::UndoHideSprites()
{
	//unhide buttons
	int spriteIndex = BUTTON_SPRITES_START;
	for (unsigned int i=0; i < buttonList.size(); i++)
	{
		PA_SetSpriteXY(0, spriteIndex, buttonList[i].originX, buttonList[i].originY);
		spriteIndex++;
	}
}

void TFViewImpl::ShowPlacePieces()
{
	PA_SetSpriteXY(1, placePiecesIndex, PLACEPIECE_POS_X, PLACEPIECE_POS_Y);
}

void TFViewImpl::HidePlacePieces()
{
	PA_SetSpriteXY(1, placePiecesIndex, -100, -100);
}

void TFViewImpl::DeselectButton()
{
	//hide selector
	PA_SetSpriteXY(0, btnSelectSpriteIndex, -50,-50);
}

void TFViewImpl::SetButtonActive(int index, bool active)
{
	for (unsigned int i=0; i < buttonList.size(); i++)
	{
		if (index == buttonList[i].index)
		{
			if (active)
			{
				PA_UpdateSpriteGfx(0, i+BUTTON_SPRITES_START, buttonList[i].spriteName);
				PA_SetSpritePal(0, i+BUTTON_SPRITES_START, BUTTONPAL);
			}
			else
			{
				PA_UpdateSpriteGfx(0, i+BUTTON_SPRITES_START, buttonList[i].inactiveSpriteName);
				PA_SetSpritePal(0, i+BUTTON_SPRITES_START, INACTIVEBUTTON_PAL);
			}
		}
	}
	
}

void TFViewImpl::HideButton(int index)
{
	for (unsigned int i=0; i < buttonList.size(); i++)
	{
		if (index == buttonList[i].index)
		{
			PA_SetSpriteXY(0, i+BUTTON_SPRITES_START, -50,-50);
		}
	}
	DeselectButton();
}